<!-- 产品 -->
<template>
  <div class="app-container">
    <!-- 检索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品系类" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入产品系类"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 操作项 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addSeries"
          v-hasPermi="['system:role:add']"
        >
          新增系列
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleProject('add')"
          v-hasPermi="['system:role:add']"
        >
          新增产品
        </el-button>
      </el-col>
    </el-row>

    <!-- 内容 -->
    <div class="product_list">
      <div
        class="product_list-item"
        v-for="(item, index) in tableList"
        :key="index"
      >
        <i class="el-icon-circle-close" @click="handleDelete(item)"></i>
        <div @click="handleProject('edit', item)">
          <img
            class="product_list-item-img"
            :src="item.picUrl.split(',')[0]"
            alt=""
          />
          <div class="product_list-item-title">
            <div>
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[8, 16, 32, 64]"
      @pagination="getList"
    />

    <!-- 新增系列 -->
    <SeriesDialog ref="seriesDialogRef" />

    <!-- 新增产品 -->
    <AddProductDialog ref="addProductDialogRef" @updateTable="resetQuery" />
  </div>
</template>
<script>
import SeriesDialog from "./components/seriesDialog.vue";
import AddProductDialog from "./components/addProductDialog.vue";
import { productList, productDelete } from "@/api/useMenu/product";
export default {
  name: "Product",
  components: { SeriesDialog, AddProductDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        seriesId: 0,
        name: undefined,
        type: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    /** 查询表格数据 */
    async getList() {
      this.loading = true;
      let response = await productList(this.queryParams);
      this.tableList = response.rows;
      this.total = response.total;
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增产品
    handleProject(enterType = "add", item = {}) {
      this.$refs.addProductDialogRef.openDialog(enterType, item);
    },
    // 新增系列
    addSeries() {
      // this.$refs.seriesDialogRef.openDialog();
      this.$router.push("/project/product/series");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id, name } = row;
      this.$modal
        .confirm('是否确认删除标题为"' + name + '"的数据项？')
        .then(function () {
          return productDelete(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.product_list {
  margin-top: 20px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  &-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 30px 20px 0 0;
    width: calc((100% - 60px) / 4);
    border-radius: 8px;
    cursor: pointer;
    &:nth-child(1) {
      margin-top: 0;
    }
    &:nth-child(2) {
      margin-top: 0;
    }
    &:nth-child(3) {
      margin-top: 0;
    }
    &:nth-child(4) {
      margin-top: 0;
    }
    &:nth-child(4n + 4) {
      margin-right: 0;
    }
    .el-icon-circle-close {
      position: absolute;
      top: 5px;
      right: 5px;
      font-size: 24px;
      display: none;
    }
    &-img {
      width: 216px;
      height: 270px;
    }
    &-title {
      margin-top: 8px;
      padding: 0 6px;
      width: 216px;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;
      div {
        text-align: center;
        word-break: break-all;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }
  &-item:hover {
    transform: scale(1.01);
    transition: all 0.4s ease 0s;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    .el-icon-circle-close {
      display: block;
      &:hover {
        color: #1890ff;
      }
    }
  }
}
</style>
